﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MxWeiXinPF.DBUtility;
using System.Data.SqlClient;
using System.Data;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.DAL
{
    public partial class addprescription
    {
        private string databaseprefix;

        public addprescription(string _databaseprefix)
        {
            this.databaseprefix = _databaseprefix;
        }

        public int Add(MxWeiXinPF.Model.addprescription model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into " + this.databaseprefix + "prescription(");
            builder.Append("wid,openid,userid,shopid,pid,url,title,remarks,status)");
            builder.Append(" values (");
            builder.Append("@wid,@openid,@userid,@shopid,@pid,@url,@title,@remarks,@status)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@wid", SqlDbType.Int), new SqlParameter("@openid", SqlDbType.VarChar, 50), new SqlParameter("@userid", SqlDbType.Int), new SqlParameter("@shopid", SqlDbType.Int), new SqlParameter("@pid", SqlDbType.Int), new SqlParameter("@url", SqlDbType.VarChar, 0xff), new SqlParameter("@title", SqlDbType.VarChar, 100), new SqlParameter("@remarks", SqlDbType.VarChar, 0x7d0), new SqlParameter("@status", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = model.wid;
            cmdParms[1].Value = model.openid;
            cmdParms[2].Value = model.userid;
            cmdParms[3].Value = model.shopid;
            cmdParms[4].Value = model.pid;
            cmdParms[5].Value = model.url;
            cmdParms[6].Value = model.title;
            cmdParms[7].Value = model.remarks;
            cmdParms[8].Value = model.status;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int AddDetail(int id, string remarks, int uid, string openid, int shopid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into " + this.databaseprefix + "bbsDetail(");
            builder.Append("remarks,uid,shopid,updatetime,bbsid)");
            builder.Append(" values (");
            builder.Append("@remarks,@uid,@shopid,@updatetime,@bbsid)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@remarks", SqlDbType.NVarChar, 0x7d0), new SqlParameter("@uid", SqlDbType.Int), new SqlParameter("@shopid", SqlDbType.Int), new SqlParameter("@updatetime", SqlDbType.DateTime), new SqlParameter("@bbsid", SqlDbType.Int) };
            cmdParms[0].Value = remarks;
            cmdParms[1].Value = uid;
            cmdParms[2].Value = shopid;
            cmdParms[3].Value = DateTime.Now;
            cmdParms[4].Value = id;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Audit(string id, string status)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@status", SqlDbType.VarChar, 10) };
            parameters[0].Value = id;
            parameters[1].Value = status;
            return DbHelperSQL.boolRunProcedure("sp_PrescriptionAudit", parameters);
        }

        public bool Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from " + this.databaseprefix + "prescription ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from " + this.databaseprefix + "prescription");
            builder.Append(" where id=@id ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" *  ");
            builder.Append(" FROM " + this.databaseprefix + "prescription ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * FROM " + this.databaseprefix + "prescription");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(builder.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, builder.ToString(), filedOrder));
        }

        public DataSet GetListByView(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" *  ");
            builder.Append(" FROM v_prescription");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public MxWeiXinPF.Model.addprescription GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from " + this.databaseprefix + "prescription ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            MxWeiXinPF.Model.addprescription addprescription = new MxWeiXinPF.Model.addprescription();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    addprescription.id = int.Parse(set.Tables[0].Rows[0]["id"].ToString());
                }
                if (set.Tables[0].Rows[0]["wid"].ToString() != "")
                {
                    addprescription.wid = int.Parse(set.Tables[0].Rows[0]["wid"].ToString());
                }
                addprescription.title = set.Tables[0].Rows[0]["title"].ToString();
                addprescription.remarks = set.Tables[0].Rows[0]["remarks"].ToString();
                addprescription.openid = set.Tables[0].Rows[0]["openid"].ToString();
                addprescription.status = set.Tables[0].Rows[0]["status"].ToString();
                addprescription.url = set.Tables[0].Rows[0]["url"].ToString();
                if (set.Tables[0].Rows[0]["userid"].ToString() != "")
                {
                    addprescription.userid = int.Parse(set.Tables[0].Rows[0]["userid"].ToString());
                }
                if (set.Tables[0].Rows[0]["shopid"].ToString() != "")
                {
                    addprescription.shopid = int.Parse(set.Tables[0].Rows[0]["shopid"].ToString());
                }
                if (set.Tables[0].Rows[0]["pid"].ToString() != "")
                {
                    addprescription.pid = int.Parse(set.Tables[0].Rows[0]["pid"].ToString());
                }
                if (set.Tables[0].Rows[0]["createtime"].ToString() != "")
                {
                    addprescription.createtime = DateTime.Parse(set.Tables[0].Rows[0]["createtime"].ToString());
                }
                if (set.Tables[0].Rows[0]["audittime"].ToString() != "")
                {
                    addprescription.audittime = DateTime.Parse(set.Tables[0].Rows[0]["audittime"].ToString());
                }
                return addprescription;
            }
            return null;
        }

        public bool Update(Model.addprescription model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_prescription set ");
            strSql.Append("status=@status");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.status;
            parameters[1].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
